<?php
class code_baseModel extends commonMod {
	public function __construct() {
		parent::__construct ();
	}
	public function search_list($table_name, $where, $order = "id desc", $return_file = NULL, $limit = NULL) {
		if (empty ( $return_file )) {
			$return_file = "*";
		}
		if (empty ( $limit )) {
			$list = $this->model->table ( $table_name )->field ( $return_file )->where ( $where )->order ( 'id desc' )->select ();
		} else {
			$list = $this->model->table ( $table_name )->field ( $return_file )->where ( $where )->limit ( $limit )->select ();
		}
		return ( array ) $list;
	}
	public function join_table($tables, $as_name = NULL, $return_file, $join, $where, $order, $limit = null) {
		if (! is_array ( $as_name )) {
			$as_name = array (
					"A",
					"B",
					"C",
					"D",
					"E",
					"F",
					"G",
					"H",
					"I" 
			);
		}
		$sql = "SELECT " . $return_file;
		if (is_array ( $tables ) && is_array ( $join ) && is_array ( $as_name )) {
			foreach ( $tables as $k => $v ) {
				if ($k == '0') {
					$sql .= " FROM {$this->model->pre}" . $v . " " . $as_name [$k];
				} else {
					$sql .= " LEFT JOIN {$this->model->pre}" . $v . " " . $as_name [$k] . " ON " . $join [$k - 1];
				}
			}
			$sql .= " WHERE {$where} ORDER BY {$order}";
			if ($limit) {
				$sql .= " LIMIT {$limit}";
			}
			$list = $this->model->query ( $sql );
			return ( array ) $list;
		} else {
			return array ();
		}
	}
	public function detail($tables, $as_name = NULL, $return_file, $join, $where) {
		if (! is_array ( $as_name )) {
			$as_name = array (
					"A",
					"B",
					"C",
					"D",
					"E",
					"F",
					"G",
					"H",
					"I" 
			);
		}
		$sql = "SELECT " . $return_file;
		if (is_array ( $tables ) && is_array ( $join ) && is_array ( $as_name )) {
			foreach ( $tables as $k => $v ) {
				if ($k == '0') {
					$sql .= " FROM {$this->model->pre}" . $v . " " . $as_name [$k];
				} else {
					$sql .= " LEFT JOIN {$this->model->pre}" . $v . " " . $as_name [$k] . " ON " . $join [$k - 1];
				}
			}
			$sql .= " WHERE {$where} ";
			$sql .= " LIMIT 0,1";
			$info = $this->model->query ( $sql );
			return ( array ) $info;
		} else {
			return array ();
		}
	}
	

}